package com.imay.live.view.custom;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.utils.ViewUtil;

import java.util.ArrayList;

/**
 * Created by liangzhenxiong on 2017/04/11 0011.
 */

public class PastseRelatvieLayout2 extends RelativeLayout {
    private Paint paint;
    private Path path;

    /**
     * 设置视图数据
     */
    public void setModel(final ArrayList<String> labels) {
        if (labels == null || labels.isEmpty()) {
            return;
        }
        TextView textLeft = ViewUtil.findView(this, R.id.tv_show_left);
        textLeft.setVisibility(GONE);
        TextView textmiddle = ViewUtil.findView(this, R.id.tv_show_mid);
        textmiddle.setVisibility(GONE);
        TextView textRight = ViewUtil.findView(this, R.id.tv_show_right);
        textRight.setVisibility(GONE);
        final int start = 0;
        int maxLenght = 10;
        for (int i = start; i < labels.size(); i++) {
            String label = labels.get(i);
            maxLenght = maxLenght - (label.length() + 1);
            if (maxLenght < 0) {
                if (i == start) {
                    textLeft.setVisibility(VISIBLE);
                    textLeft.setText(getContext().getString(R.string.label_tag_format, label));
                }
            } else {
                if (i == start) {
                    textLeft.setVisibility(VISIBLE);
                    textLeft.setText(getContext().getString(R.string.label_tag_format, label));
                } else if (i == (start + 1)) {
                    textmiddle.setVisibility(VISIBLE);
                    textmiddle.setText(getContext().getString(R.string.label_tag_format, label));
                } else if (i == (start + 2)) {
                    textRight.setVisibility(VISIBLE);
                    textRight.setText(getContext().getString(R.string.label_tag_format, label));
                }
            }
        }
    }

    public PastseRelatvieLayout2(Context context) {
        super(context);
        init(context);
    }

    public PastseRelatvieLayout2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PastseRelatvieLayout2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = 21)
    public PastseRelatvieLayout2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    void init(Context context) {
//        android:layout_height="52dp"
        View view = LayoutInflater.from(context).inflate(R.layout.item_discoverypaste2_layout, this, true);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.height = (int) getResources().getDimension(R.dimen.pasteview_height);
        view.setLayoutParams(params);
        setBackgroundColor(getResources().getColor(R.color.bg_ff));
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (isPagging) {
            if (path == null) {
                path = new Path();
                paint = new Paint();
                paint.setStyle(Paint.Style.STROKE);
                paint.setAntiAlias(true);
                paint.setStrokeWidth(Resources.getSystem().getDisplayMetrics().density * 8);
                paint.setColor(getResources().getColor(R.color.bg_f5));
            }
            path.reset();
            path.moveTo(0, (float) (canvas.getHeight() + 0.1));
            path.lineTo(canvas.getWidth(), (float) (canvas.getHeight() + 0.1));
            canvas.drawPath(path, paint);
        }
    }

    boolean isPagging = false;

    public void setShowPagging() {
        isPagging = true;
        requestLayout();
    }
}
